package cn.kmsoft.jssc.config;

import cn.kmsoft.jssc.entity.analyse.AnalyseDb;
import cn.kmsoft.jssc.entity.analyse.AnalysePoint;
import cn.kmsoft.jssc.entity.analyse.AnalyseServer;
import cn.kmsoft.jssc.entity.analyse.AnalyseTable;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : cqwensten
 * @description : todo
 * @email : cqwensten@163.com
 * @date : 2023/2/17
 */
@Configuration
@ConditionalOnExpression("!'${analyst}'.isEmpty()")
@ConfigurationProperties(prefix = "analyst")
public class AnalyseConfig {
    //Python服务地址
    private String pythondomain;
    private String pythonalonedomain;

    //分析方案
    private String profileScheme;

    //系统路径
    private String projectpath; //工程根路径
    private String testshppath; //测试shp路径
    private String dlhyOriginalFolder;//地类还原分析原始数据路径
    private String dlhyResultFolder;//地类还原分析结果数据路径

    private String zipshapefiles;
    private String shapefileType;

    //后台微服务地址
    //private String serverUrl; //分析服务地址

    //key为：py,pl,pt
    private Map<String, String> analystTypes = new HashMap<String,String>();
    private List<String> typepoints;//py、pl、pt、diff
    private List<String> typenames; //intersectpy,intersectpl,intersectpt,difference

    private List<AnalyseServer> analyseServers;//分析服务器的列表
    private List<AnalyseDb> analyseDbs;//数据库列表
    private List<AnalysePoint> analysePoints;//分析点列表
    private List<AnalyseTable> analyseTables;//目标数据表列表

    private Map<String,String> tdlyxzfldm;//土地利用现状分类代码
    private Map<String,String> tdghytfldm;//土地规划用途分类代码
    private Map<String,String> tdlyxzfldm_sd;//土地利用现状分类代码-三调
    private TdlyfldmConfig tdlyfldmConfig = new TdlyfldmConfig();

    private Map<String,String> defaultAnalyses; //默认的分析点
    private Map<String,String> tdlyxz_fldm2mc_sd; //土地利用现状代码映射表-三调

    public Map<String, String> getTdlyxz_fldm2mc_sd() {
        return tdlyxz_fldm2mc_sd;
    }
    public void setTdlyxz_fldm2mc_sd(Map<String, String> tdlyxz_fldm2mc_sd) {
        this.tdlyxz_fldm2mc_sd = tdlyxz_fldm2mc_sd;
    }

    public String getZipshapefiles() {
        return zipshapefiles;
    }
    public void setZipshapefiles(String zipshapefiles) {
        this.zipshapefiles = zipshapefiles;
    }

    public String getShapefileType() {
        return shapefileType;
    }
    public void setShapefileType(String shapefileType) {
        this.shapefileType = shapefileType;
    }

    //分析方案
    public String getProfileScheme() {
        return profileScheme;
    }
    public void setProfileScheme(String profileScheme) {
        this.profileScheme = profileScheme;
    }

    public String getProjectpath() {
        return projectpath;
    }
    public void setProjectpath(String projectpath) {
        this.projectpath = projectpath;
    }

    public String getTestshppath() {
        return testshppath;
    }
    public void setTestshppath(String testshppath) {
        this.testshppath = testshppath;
    }

    public String getDlhyOriginalFolder() {
        return dlhyOriginalFolder;
    }
    public void setDlhyOriginalFolder(String dlhyOriginalFolder) {
        this.dlhyOriginalFolder = dlhyOriginalFolder;
    }

    public String getDlhyResultFolder() {
        return dlhyResultFolder;
    }
    public void setDlhyResultFolder(String dlhyResultFolder) {
        this.dlhyResultFolder = dlhyResultFolder;
    }

    public String getPythondomain() {
        return pythondomain;
    }
    public void setPythondomain(String pythondomain) {
        this.pythondomain = pythondomain;
    }

    public String getPythonalonedomain() {
        return pythonalonedomain;
    }
    public void setPythonalonedomain(String pythonalonedomain) {
        this.pythonalonedomain = pythonalonedomain;
    }

    /*
    public String getServerUrl() {
        return serverUrl;
    }
    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }
    *
    */


    public List<AnalyseServer> getAnalyseServers() {
        return analyseServers;
    }
    public void setAnalyseServers(List<AnalyseServer> analyseServers) {
        this.analyseServers = analyseServers;
    }

    public List<AnalyseDb> getAnalyseDbs() {
        return analyseDbs;
    }
    public void setAnalyseDbs(List<AnalyseDb> analyseDbs) {
        this.analyseDbs = analyseDbs;
    }

    public List<AnalysePoint> getAnalysePoints() {
        return analysePoints;
    }
    public void setAnalysePoints(List<AnalysePoint> analysePoints) {
        this.analysePoints = analysePoints;
    }

    public List<AnalyseTable> getAnalyseTables() {
        return analyseTables;
    }
    public void setAnalyseTables(List<AnalyseTable> analyseTables) {
        this.analyseTables = analyseTables;
    }

    public Map<String, String> getAnalystTypes() {
        //构建analystTypes
        if(this.typepoints.size()<=this.typenames.size()) {
            for (int i = 0; i < typepoints.size(); i++) {
                String typepoint = this.typepoints.get(i);
                String typename = this.typenames.get(i);
                if( !this.analystTypes.containsKey(typepoint)) {
                    this.analystTypes.put(typepoint, typename);
                }
            }
        }
        return analystTypes;
    }

    public List<String> getTypepoints() {
        return typepoints;
    }
    public void setTypepoints(List<String> typepoints) {
        this.typepoints = typepoints;
    }

    public List<String> getTypenames() {
        return typenames;
    }
    public void setTypenames(List<String> typenames) {
        this.typenames = typenames;
    }

    public Map<String, String> getTdlyxzfldm() {
        return tdlyxzfldm;
    }
    public void setTdlyxzfldm(Map<String, String> tdlyxzfldm) {
        this.tdlyxzfldm = tdlyxzfldm;
        //初始化tdlyfldmConfig
        this.tdlyfldmConfig.initTdlyfldm(tdlyxzfldm);
    }

    public Map<String, String> getTdghytfldm() {
        return tdghytfldm;
    }
    public void setTdghytfldm(Map<String, String> tdghytfldm) {
        this.tdghytfldm = tdghytfldm;
        //初始化tdlyfldmConfig
        this.tdlyfldmConfig.initTdghfldm(tdghytfldm);
    }

    public Map<String, String> getTdlyxzfldm_sd() {
        return tdlyxzfldm_sd;
    }
    public void setTdlyxzfldm_sd(Map<String, String> tdlyxzfldm_sd) {
        this.tdlyxzfldm_sd = tdlyxzfldm_sd;
        //初始化tdlyfldmConfig
        this.tdlyfldmConfig.initTdlyfldm_sd(tdlyxzfldm_sd);
    }

    public TdlyfldmConfig getTdlyfldmConfig() {
        return tdlyfldmConfig;
    }


    public Map<String, String> getDefaultAnalyses() {
        return defaultAnalyses;
    }
    public void setDefaultAnalyses(Map<String, String> defaultAnalyses) {
        this.defaultAnalyses = defaultAnalyses;
    }





    /**
     * 根据serverId获取对应的分析服务器信息
     *
     * @param serverId 默认是
     * @return
     */
    public AnalyseServer getAnalyseServerByServerId(String serverId){
        AnalyseServer result=null;
        for(AnalyseServer analyseServer: this.analyseServers){
            if(serverId.equalsIgnoreCase(analyseServer.getServerId())){
                result=analyseServer;
                break;
            }
        }
        return result;
    }

    /**
     * 根据dbkey获取对应的数据库信息
     * @param dbkey 默认是sde
     * @return
     */
    public AnalyseDb getAnalyseDbByDbkey(String dbkey){
        AnalyseDb result=null;
        for(AnalyseDb analysedb: this.analyseDbs){
            if(dbkey.equalsIgnoreCase(analysedb.getDbkey())){
                result=analysedb;
                break;
            }
        }
        return result;
    }

    /**
     * 根据分析点编码code获取对应的分析点信息
     * @param code  例如dltb、yjjbnt、stbhhx等
     * @return
     */
    public AnalysePoint getAnalysePointByCode(String code){
        AnalysePoint result=null;
        for(AnalysePoint analysePoint: this.analysePoints){
            if(code.equalsIgnoreCase(analysePoint.getCode())){
                result = analysePoint;
                break;
            }
        }
        return result;
    }

    /**
     * 根据分析点编码筛选数据表
     * @param analysePoint   例如dltb
     * @return
     */
    public List<AnalyseTable> selectAnalyseTableByAnalysePoint(String analysePoint){
        List<AnalyseTable> results = new ArrayList<AnalyseTable>();
        for(AnalyseTable analyseTable: this.analyseTables){
            if(analysePoint.equalsIgnoreCase(analyseTable.getAnalysePoint())){
                results.add(analyseTable);
            }
        }
        return results;
    }

    /**
     * 根据dbKey和tablename获取分析表信息
     * @param dbKey
     * @param tablename
     * @return
     */
    public AnalyseTable getAnalyseTableByTablename(String dbKey,String tablename){
        AnalyseTable result = null;
        for(AnalyseTable analyseTable: this.analyseTables){
            if(dbKey.equalsIgnoreCase(analyseTable.getDbKey()) && tablename.equalsIgnoreCase(analyseTable.getTableName())){
                result = analyseTable;
                break;
            }
        }
        return result;
    }

    /**
     * 根据analystCode和defaultYear获取对应年份的分析表信息
     * @param analystCode
     * @param defaultYear
     * @return
     */
    public AnalyseTable getAnalyseTableByAnalystCodeAndYear(String analystCode,String defaultYear){
        AnalyseTable result = null;
        for(AnalyseTable analyseTable: this.analyseTables){
            if(analystCode.equalsIgnoreCase(analyseTable.getAnalysePoint()) && defaultYear.equalsIgnoreCase(analyseTable.getYear())){
                result = analyseTable;
                break;
            }
        }
        return result;
    }

    /**
     * 根据analystCode获取所有的分析表信息
     * @param analystCode
     * @return
     */
    public List<AnalyseTable> getAllAnalyseTableByAnalystCode(String analystCode){
        List<AnalyseTable> results = new ArrayList<AnalyseTable>();
        for(AnalyseTable analyseTable: this.analyseTables){
            if(analystCode.equalsIgnoreCase(analyseTable.getAnalysePoint()) ){
                results.add(analyseTable);
            }
        }
        return results;
    }

    /**
     * 根据分析点代码获取默认数据分析的数据年份
     * @param analystCode
     * @return
     */
    public String getDefaultYearByAnalystCode(String analystCode){
        String defaultYear = null;
        for(AnalysePoint analysePoint: this.getAnalysePoints()){
            if(analystCode.equalsIgnoreCase(analysePoint.getCode()) ){
                defaultYear = analysePoint.getDefaultYear();
                break;
            }
        }
        return defaultYear;
    }

    /**
     * 根据数据年份获取dltb地类图斑的分析表
     * @param yearStr
     * @return
     */
    public AnalyseTable getDltbAnalyseTableByYear(String yearStr){
        AnalyseTable result = null;
        for(AnalyseTable analyseTable: this.analyseTables){
            if("dltb".equalsIgnoreCase(analyseTable.getAnalysePoint()) && yearStr.equalsIgnoreCase(analyseTable.getYear())){
                result = analyseTable;
                break;
            }
        }
        return result;
    }


}
